GBML manual / Lists

list_remove

Removes the item at a POSITION in target - not a value. list_remove(l, 0) removes the FIRST item, whatever it is; it does not look for an item equal to 0. (list_add takes a value, so these two read alike but do not match.) Returns true if it removed something, false if the position was out of range (target is left unchanged). Everything after the removed item shifts down one place, so target stays in order.

Syntax

list_remove(target, index) -> bool

Arguments

NameType
targetlist
indexint

Returns

bool

Description

Removes the item at index (0 is the first item) from target. Returns true if it removed something, false if index was out of range - target is left unchanged either way it fails, never a crash.

Everything after the removed item shifts down one place, so target stays in the same order it was already in. This is deliberate: for an inventory or a high-score table, order is usually the whole point, and a removal that reordered the rest of the list would be a worse surprise than the small extra cost of shifting. It does mean removing near the FRONT of a long, nearly-full list is more work than removing near the back - rarely something to worry about at the sizes this hardware ever holds.

Example

/// @type {list<int>}
var scores = [10, 20, 30];
var removed = list_remove(scores, 0); // true - scores is now [20, 30]
A real capture - the list with all three scores still in it, before the removeA real capture - the list with all three scores still in it, before the remove
A real capture - the list with all three scores still in it, before the remove
The same list after list_remove(scores, 0) - 20 and 30 shifted down, 10 is goneThe same list after list_remove(scores, 0) - 20 and 30 shifted down, 10 is gone
The same list after list_remove(scores, 0) - 20 and 30 shifted down, 10 is gone

On the GBA

Order-preserving removal (the vector's erase), not the cheaper "swap the last item into the gap" trick some libraries use - deliberate, so a high-score table or an inventory never silently reorders itself. That makes this O(n) in how many items are AFTER index, same as a shift-based array-delete or any other shift-based removal - fine for the small, bounded lists this hardware ever holds, but worth knowing if you remove from the front of a nearly-full list every frame.

See also: list_addlist_insertlist_lengthlist_clearlist_containsData containerslist_filter